#ifndef __KJSON_H__
#define __KJSON_H__
#include <stdlib.h>
#include <assert.h>
// JSON type
#define KJsonType int
#define KJSON_NULL 1
#define KJSON_NUM 2
#define KJSON_STRING 3
#define KJSON_ARRAY 4
#define KJSON_OBJECT 5
#define KJSON_TRUE 6
#define KJSON_FALSE 7
#define kjson_get_type(x) x->type 
#define EXPECT(x, ac) assert(ac == x)

// 错误码
typedef enum{
    KJSON_PARSE_OK = 0,
    KJSON_EXPECT_VALUE,
    KJSON_INVALID_SYNTAX
}KJSON_ERRCODE;

typedef union KJsonData{
    double num;
    const char * str;   
}KJsonData;
// json对象
typedef struct KJsonVal{
    int type;
    KJsonData data;
}* KJsonVal;

// json数字类型
typedef struct KJsonNum{
    double value;
}* KJsonNum;

// json上下文环境
typedef struct KJsonContex{
    const char * str;
}KJsonContex;

// json解析
KJSON_ERRCODE kjson_parse(KJsonVal val, const char * str);

KJSON_ERRCODE kjson_parse_true(KJsonVal val, KJsonContex* contex);
KJSON_ERRCODE kjson_parse_val(KJsonVal val, KJsonContex* contex);
KJSON_ERRCODE kjson_parse_false(KJsonVal val, KJsonContex* contex);
KJSON_ERRCODE kjson_parse_null(KJsonVal val, KJsonContex* contex);
KJSON_ERRCODE kjson_parse_whitespace(KJsonContex* contex);
KJSON_ERRCODE kjson_parse_num(KJsonVal val, KJsonContex* contex);

double kjson_get_num(KJsonVal val);
#endif
